package com.shop2cn.iapi.prodstock.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.prodstock.parameter.InitProductActivityStockBean;
import com.shop2cn.iapi.prodstock.service.InitProductActivityStockCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallServiceV2;
import com.ymttest.database.model.GodProductActivityStock;
import com.ymttest.database.sqlwapper.GodProductActivityStockWapper;
import com.ymttest.utils.AgentUsers;

/**
 * 初始化活动库存Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_InitProductActivityStock {

	private static InitProductActivityStockBean initproductactivitystockBean;
	private static InitProductActivityStockCall initproductactivitystockCall;

	@BeforeClass
	public static void setUp() {
		
		Logger.createResultFile("初始化活动库存Test");
	}

	@Before
	public void caseUp() {
		initproductactivitystockBean = new InitProductActivityStockBean();
		initproductactivitystockCall = new InitProductActivityStockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(initproductactivitystockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";


	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_InitProductActivityStock_001() {
		Logger.start(true, "创建商品，使用是个活动库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,10, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			//验证入库
			equalsDB(initProductaddactivity, activityId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_InitProductActivityStock_002() {
		Logger.start(true, "创建商品，使用规格库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,0,null, false);
			initProductaddactivity.setStockType(0);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			//验证入库
			equalsDB(initProductaddactivity, activityId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_InitProductActivityStock_003() {
		Logger.start(true, "修改活动，把规格库存换成活动库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,0,null, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			AddactivityBean initProductaddactivity1 = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,10, false);
			initProductaddactivity1.setActivityId(activityId);
			Integer activityId1 = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity1);
			//验证入库
			equalsDB(initProductaddactivity1, activityId1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_InitProductActivityStock_004() {
		Logger.start(true, "修改活动，把活动库存换成规格库存");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			
			AddactivityBean initProductaddactivity1 = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,10, false);
			Integer activityId1 = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity1);
			
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,0,null, false);
			initProductaddactivity.setActivityId(activityId1);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			//验证入库
			equalsDB(initProductaddactivity, activityId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_InitProductActivityStock_005() {
		Logger.start(true, "修改活动，删除规格");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,10, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			initProductaddactivity.setActivityId(activityId);
			initProductaddactivity.getProductList().get(0).getCatalogList().remove(0);
			activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			//验证入库
			equalsDB(initProductaddactivity, activityId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_InitProductActivityStock_006() {
		Logger.start(true, "使用规格库存的活动，删除规格");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			//创建限时抢活动
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,0,null, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			initProductaddactivity.setActivityId(activityId);
			initProductaddactivity.getProductList().get(0).getCatalogList().remove(0);
			activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			//验证入库
			equalsDB(initProductaddactivity, activityId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	public void equalsDB(AddactivityBean initProductaddactivity,Integer activityId) {
		initProductaddactivity.getProductList().forEach(a->{
			a.getCatalogList().forEach(b->{
				GodProductActivityStock s = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(a.getProductId(),b.getCatalogId());
				Logger.verifyEquals(a.getProductId(), s.getsProductID(), "验证商品id");
				Logger.verifyEquals(b.getCatalogId(), s.getsCatalogID(), "验证规格id");
				Logger.verifyEquals(activityId, s.getiActivityID(), "验证活动id");
				Logger.verifyEquals(b.getActivityStock().doubleValue(), s.getiStockNum().doubleValue(), "验证库存");
				Logger.verifyEquals(b.getDiscountPrice().doubleValue(), s.getfActivityPrice().doubleValue(), "验证活动价");
				Logger.verifyEquals(initProductaddactivity.getSellerId(), s.getSellerId(), "验证商家id");
				Logger.verifyEquals(3, s.getActivityType(), "验证活动类型");
				Logger.verifyEquals(initProductaddactivity.getStockType(), s.getStockType(), "验证库存类型");
			});
		});
	}
	
}